<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//站点相关信息
class Column extends MY_Controller {
	public function __construct(){
		parent::__construct();
		//权限处理
		$this->sess_checked();
		//加载栏目模型类
        $this->load->model('column_m');
        $this->load->model('sites_m');
        $this->load->model('info_m');
	}
	public function lists(){
		//获取站点下拉列表
		$sites_where['status']=array('=',1);
		$sitesselected=$this->sites_m->selall($sites_where);
		if(!$sitesselected){ 
			echo Js::alert('请先添加站点');exit;
		}
		if(isset($_GET['sitesid'])){
			$default_sitesid=$_GET['sitesid'];
		}elseif(isset($_POST['sitesid'])){
			$default_sitesid=$_POST['sitesid'];
		}else{
			$default_sitesid=$sitesselected[0]['id'];
		}
		
		$this->assign('default_sitesid',$default_sitesid);
		$this->assign('sitesselected',$sitesselected);
		//栏目列表
		$where['A.sitesid']=array('=',$default_sitesid);
		$data=$this->column_m->sleAllTable($where,'A.*,B.name as sitesname,');
		$dataAll=$this->column_m->selall(array(),'colid,name');
		foreach($data as $k=>$v){
			if($v['iscopy']==1){
				foreach($dataAll as $x=>$y){
					if($v['copyid']==$y['colid']){
						$v['copycolumnname']=$y['name'];//共享栏目名称
						break;
					}
				}
			}else{
			 	$v['copycolumnname']='';
			}
			$data[$k]=$v;
		}

		$list=$this->column_m->getsortdata($data);
		$this->assign('list',$list); 
		if(isset($_POST['sitesid'])){
			$this->display('column/columnlist.html');
		}else{
			$this->display('column/index.html');
		}
	}
	//栏目数据验证
	private function column_data($data){
		if(!isset($data['name'])){ajax_return(0,'数据错误');}
		if(trim($data['name'])==''){ajax_return(0,'栏目名称不能为空');}
		if($data['coltype']==2){
			if(trim($data['collisttemplate'])==''){ajax_return(0,'外链地址不能为空');}
		}
		if($data['coltype']==1){//系统栏目才有拷贝栏目
			if($data['iscopy']==1 && $data['copyid']==''){ajax_return(0,'请选择拷贝栏目');}
			if($data['iscopy']==0){$data['copyid']='';}
		}
		return $data;
	}
	//获取拷贝栏目-
	private function getiscopyslected($sitesid,$coltype,$copyid=''){
		if($coltype==0){//分类栏目不能拷贝
				$getiscopyslected='<option data-sites="0" value="">分类栏目不能拷贝</option>';
		}else{//系统栏目可以拷贝
			$column_where_iscopy['coltype']=array('in',array(0,1));
			$column_where_iscopy['sitesid']=array('!=',$sitesid);
			$data=$this->column_m->sleAllTable($column_where_iscopy,'A.*,B.name as sitesname');
			if(empty($data)){
				$getiscopyslected='<option data-sites="0" value="">当前未有可以拷贝栏目存在</option>';
			}else{
				$noselectedpid=array('coltype',array(0));
				$getiscopyslected=$this->column_m->getCopyslected($data,$copyid,$noselectedpid);
			}
		}
		return $getiscopyslected;
	}
	//获取父级栏目
	private function getslected($sitesid,$iscurcolid=false,$defaultcolumnid=''){//$defaultcolumnid 默认选中；
			//所有分类栏目
			if($iscurcolid){//编辑时去掉当前栏目；
				$column_where['colid']=array('!=',$iscurcolid);
			}
			$column_where['coltype']=array('=',0);
			$column_where['sitesid']=array('=',$sitesid);
			$data=$this->column_m->selall($column_where,'colid,parentid,sitesid,name,iscopy');
			//获取父级下拉栏目列表
			$getslected='';
			$getslected.='<option data-sites="0" value="0">顶级栏目</option>';
			$getslectedvalue=$this->column_m->getslected($data,$defaultcolumnid);
			if($getslectedvalue){
				$getslected.=$getslectedvalue;
			}
			return $getslected;
	}
	//添加栏目
	public function add(){
		if(isset($_POST['iscopycolumn'])){//根据站点选择 获取 拷贝栏目 和 父级栏目
			$data=array('copyslected'=>'','selected'=>'');
			$data['copyslected']=$this->getiscopyslected($_POST['sitesid'],$_POST['coltype']);
			$data['selected']=$this->getslected($_POST['sitesid']);
			ajax_return(1,'',$data);
		}elseif(isset($_POST['type']) && $_POST['sitesid']){//添加栏目类型和当前站点
			$type=$_POST['type'];
			$sitesid=$_POST['sitesid'];
			$this->assign('sitesid',$sitesid);
			//type 0 分类 1.系统 2.外链
			$this->assign('type',$type);
			//获取站点下拉列表
			$sites_where['status']=array('=',1);
			$sitesselected=$this->sites_m->selall($sites_where);
			$this->assign('sitesselected',$sitesselected);
			
			//获取父级下拉栏目列表
			$getslected=$this->getslected($sitesid);
			$this->assign('getslected',$getslected);

			
			
			if($type==0){
				echo $this->fetch('column/comadd/typeadd.html');
			}elseif($type==1){
				//系统栏目 获取拷贝栏目
				$getiscopyslected=$this->getiscopyslected($sitesid,1);
				$this->assign('getiscopyslected',$getiscopyslected);
				echo $this->fetch('column/comadd/sysadd.html');
			}elseif($type==2){
				echo $this->fetch('column/comadd/linkadd.html');
			}
		}elseif(isset($_GET['sitesid']) || isset($_GET['addcolumn'])){//列表添加栏目
			$type=0;
			$sitesid=$_GET['sitesid'];
			$this->assign('coltype',$type);
			$this->assign('sitesid',$sitesid);
			//type 0 分类 1.系统 2.外链
			$this->assign('type',$type);
			//获取站点下拉列表
			$sites_where['status']=array('=',1);
			$sitesselected=$this->sites_m->selall($sites_where);
			$this->assign('sitesselected',$sitesselected);
			
			//获取父级下拉栏目列表
			$getslected=$this->getslected($sitesid);
			$this->assign('getslected',$getslected);

			$this->display('column/add.html');
			
		}elseif(isset($_POST['ajaxs'])){//添加栏目
			$data=form_ajaxs($_POST['formjquery']);
			$data=$this->column_data($data);
			//获取栏目深度
			if($data['parentid']==0){$data['depth']=1;}else{
				$where['colid']=array('=',$data['parentid']);
				$list=$this->column_m->sel($where,'depth');
				$data['depth']=intval($list['depth'])+1;
			}

			//同个站点同级栏目不能存在相同栏目名称
			$isexits_name['name']=array('=',$data['name']);
			$isexits_name['parentid']=array('=',$data['parentid']);
			$isexits_name['sitesid']=array('=',$data['sitesid']);
			if($this->column_m->sel($isexits_name,'colid')){
				ajax_return(0,'当前站点，当前同级已经存在该栏目名');
			}
			
			if($addid=$this->column_m->add($data)){
				$this->adminuserRecord_m->add(1,1,'栏目id为'.$addid);
				ajax_return(1,'添加栏目成功');
			}else{
				$this->adminuserRecord_m->add(1,2,'栏目');
				ajax_return(0,'添加栏目失败');
			}
		}else{
			echo Js::alert('前选择栏目类型');exit;
		}
	}
	public function edit(){
		if(isset($_POST['editorderid'])){//编辑排序
			$where['colid']=array('=',$_POST['colid']);
			$data['orderid']=$_POST['orderid'];
			if($this->column_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(2,1,'栏目id为'.$_POST['colid']."修改排序为".$_POST['orderid']);
				ajax_return(1,"修改排序成功");
			}else{
				$this->adminuserRecord_m->add(2,2,'栏目id为'.$_POST['colid']."修改排序为".$_POST['orderid']);
				ajax_return(0,'修改排序失败');
			}
		}elseif(isset($_POST['ajaxs'])){//更改
			$data=form_ajaxs($_POST['formjquery']);
			$data=$this->column_data($data);
			$where['colid']=array('=',$data['colid']);
			//获取栏目深度
			if($data['parentid']==0){$data['depth']=1;}else{
				$where_p['colid']=array('=',$data['parentid']);
				$list=$this->column_m->sel($where_p,'depth');
				$data['depth']=intval($list['depth'])+1;
			}
			if($this->column_m->edit($where,$data)!==false){
				$this->adminuserRecord_m->add(2,1,'栏目id为'.$data['colid']);
				ajax_return(1,"提交成功");
			}else{
				$this->adminuserRecord_m->add(2,2,'栏目id为'.$data['colid']);
				ajax_return(0,'提交失败');
			}

		}elseif(isset($_GET['colid'])){//编辑内容显示
			$where['colid']=array('=',$_GET['colid']);
			$list=$this->column_m->sel($where);
			$sitesid=$list['sitesid'];
			$this->assign('list',$list);

			//获取站点下拉列表
			$sites_where['status']=array('=',1);
			$sitesselected=$this->sites_m->selall($sites_where);
			$this->assign('sitesselected',$sitesselected);

			//获取父级下拉栏目列表
			$getslected=$this->getslected($sitesid,$list['colid'],$list['parentid']);
			$this->assign('getslected',$getslected);
			if($list['coltype']==1){
				//系统栏目 获取拷贝栏目
				$getiscopyslected=$this->getiscopyslected($sitesid,1,$list['copyid']);
				$this->assign('getiscopyslected',$getiscopyslected);
			}
			$this->display('column/edit.html');

		}else{
			echo Js::alert('参数错误');exit;
		}
	}
	//删除栏目
	public function del(){
		if(isset($_POST['colid'])){
			$colid=$_POST['colid'];
			$where['colid']=array('=',$colid);
			if($this->info_m->sel($where,'infoid')){
				ajax_return(0,'当前栏目下存在文章，不能删除');
			}
			//判断是否存在下级栏目
			$next_where['parentid']=array('=',$colid);
			if($this->column_m->sel($next_where,'colid')){
				ajax_return(0,'存在下级栏目,不能删除');
			}

			if($this->column_m->del($where)===false){
				$this->adminuserRecord_m->add(3,2,'栏目id为'.$colid);
				ajax_return(0,'删除失败');
			}else{
				$this->adminuserRecord_m->add(3,1,'栏目id为'.$colid);
				ajax_return(1,'删除成功');
			}

		}else{
			ajax_return(0,'参数错误');
		}
	}
	
}
